package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.utils.NumberUtil;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @auther: bin.liu
 * @Date: 2019/1/28 11:07
 * @Description: 周报月报季度报工单完成率
 */
@RestController
@RequestMapping("/completedOrdersPercentAPI")
public class CompletedOrdersPercentAPI {

    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private RepairOrderBiz repairOrderBiz;
    @Autowired
    private TaskOrderBiz taskOrderBiz;
    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;

    @RequestMapping("/getOrdersPercent")
    public JSONObject getOrdersPercent(String type,String enterpriseId){
        JSONObject result = new JSONObject();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        Date startDateTime = new Date();
        Date endDateTime = new Date();
        Date now = new Date();
        JSONArray dateTimes = new JSONArray();
        List<String> newDates = new ArrayList<>();

        JSONObject percentObject = new JSONObject();
        JSONArray dailyAllOrders = new JSONArray();
        JSONArray ppmAllOrders = new JSONArray();
        JSONArray mrAllOrders = new JSONArray();
        JSONArray taskAllOrders = new JSONArray();

        JSONArray dailyCompletedOrders = new JSONArray();
        JSONArray ppmCompletedOrders = new JSONArray();
        JSONArray mrCompletedOrders = new JSONArray();
        JSONArray taskCompletedOrders = new JSONArray();

        JSONArray allCompleteArray = new JSONArray();
        JSONArray allTotalArray = new JSONArray();
        //按企业下的各模块来计算图表
        QueryParam queryParam1 = new QueryParam("-1", "-1");
        queryParam1.getSqlMap().put("enterpriseId", enterpriseId);
        List<ApplicationModule> applicationModules = applicationModuleBiz.findList(queryParam1);
        if ("month".equals(type)) {
            startDateTime = c.getTime();
            endDateTime = now;
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "M/d");
        } else if ("week".equals(type)) {
            startDateTime = DateTimeUtils.getFirstDateOfWeek(now);
            endDateTime = now;
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "MM-dd");
        } else if ("quarter".equals(type)) {
            int season = DateTimeUtils.getSeason(now);
            for (int i = 1; i <= season; i++) {
                newDates.add(NumberUtil.convert(i) + "季度");
                // 每个季度的发单数，完成数
                for (ApplicationModule x : applicationModules){
                    if (x.getRemark().equals("MAINTAIN")){
                        ppmAllOrders = maintainOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                        ppmCompletedOrders = maintainOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);

                    } else if(x.getRemark().equals("INSPECTION")){
                        dailyAllOrders = inspectionOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                        dailyCompletedOrders = inspectionOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);

                    } else if(x.getRemark().equals("REPAIR")) {
                        mrAllOrders = repairOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                        mrCompletedOrders = repairOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);
                    }
                    else if(x.getRemark().equals("TASK")) {
                        taskAllOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                        taskCompletedOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);
                    }
                }
                int dailyAllTotal = 0;
                int dailyAllCompleted = 0;
                if (dailyAllOrders.size() > 0) {
                    for (int d = 0; d < dailyAllOrders.size(); d++) {
                        JSONArray completeObject = dailyCompletedOrders.getJSONArray(d);
                        JSONArray allObject = dailyAllOrders.getJSONArray(d);
                        dailyAllTotal  += allObject.getIntValue(1);
                        dailyAllCompleted += completeObject.getIntValue(1);
                    }
                }
                int ppmAllTotal = 0;
                int ppmAllCompleted = 0;
                if (ppmAllOrders.size() > 0) {
                    for (int p = 0; p < ppmAllOrders.size(); p++) {
                        JSONArray completeObject = ppmCompletedOrders.getJSONArray(p);
                        JSONArray allObject = ppmAllOrders.getJSONArray(p);
                        ppmAllTotal  += allObject.getIntValue(1);
                        ppmAllCompleted += completeObject.getIntValue(1);
                    }
                }
                int mrAllTotal = 0;
                int mrAllCompleted = 0;
                if (mrAllOrders.size() > 0) {
                    for (int m = 0; m < mrAllOrders.size(); m++) {
                        JSONArray completeObject = mrCompletedOrders.getJSONArray(m);
                        JSONArray allObject = mrAllOrders.getJSONArray(m);
                        mrAllTotal  += allObject.getIntValue(1);
                        mrAllCompleted += completeObject.getIntValue(1);
                    }
                }
                int taskAllTotal = 0;
                int taskAllCompleted = 0;
                if (taskAllOrders.size() > 0) {
                    for (int m = 0; m < mrAllOrders.size(); m++) {
                        JSONArray completeObject = taskCompletedOrders.getJSONArray(m);
                        JSONArray allObject = taskAllOrders.getJSONArray(m);
                        taskAllTotal  += allObject.getIntValue(1);
                        taskAllCompleted += completeObject.getIntValue(1);
                    }
                }
                int allTotal = dailyAllTotal + ppmAllTotal + mrAllTotal+taskAllTotal;
                int allCompleted = dailyAllCompleted + ppmAllCompleted + mrAllCompleted+taskAllCompleted;
                if (allTotal == 0) {
                    allCompleteArray.add(0);
                } else {
                    allCompleteArray.add((allCompleted * 100 / allTotal * 100) / 100);
                }
                allTotalArray.add(allTotal);

                dateTimes.addAll(newDates);
            }
        }

        if (!"quarter".equals(type)) {
            dateTimes.addAll(newDates);
            List<String> dates = DateTimeUtils.getDates(startDateTime, endDateTime, "yyyy-MM-dd");
            // 每个季度的发单数，完成数
            for (ApplicationModule x : applicationModules) {
                if ("MAINTAIN".equals(x.getRemark())) {
                    ppmAllOrders = maintainOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, false);
                    ppmCompletedOrders = maintainOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, true);

                } else if ("INSPECTION".equals(x.getRemark())) {
                    dailyAllOrders = inspectionOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, false);
                    dailyCompletedOrders = inspectionOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, true);

                } else if ("REPAIR".equals(x.getRemark())) {
                    mrAllOrders = repairOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, false);
                    mrCompletedOrders = repairOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, true);
                }
                else if ("TASK".equals(x.getRemark())) {
                    taskAllOrders = taskOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, false);
                    taskCompletedOrders = taskOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, true);
                }
            }

            for (int i = 0; i < dates.size(); i++) {
                int total1 = 0;
                int complete1 = 0;
                if (dailyAllOrders.size() > 0) {
                    // Daily Check巡检
                    JSONArray allObject1 = dailyAllOrders.getJSONArray(i);
                    JSONArray completeObject1 = dailyCompletedOrders.getJSONArray(i);
                    total1 = allObject1.getIntValue(1);
                    complete1 = completeObject1.getIntValue(1);
                }
                int total2 = 0;
                int complete2 = 0;
                if (ppmAllOrders.size() > 0) {
                    // PPM维保
                    JSONArray allObject2 = ppmAllOrders.getJSONArray(i);
                    JSONArray completeObject2 = ppmCompletedOrders.getJSONArray(i);
                    total2 = allObject2.getIntValue(1);
                    complete2 = completeObject2.getIntValue(1);
                }
                int total3 = 0;
                int complete3 = 0;
                if (mrAllOrders.size() > 0) {
                    JSONArray allObject3 = mrAllOrders.getJSONArray(i);
                    JSONArray completeObject3 = mrCompletedOrders.getJSONArray(i);
                    total3 = allObject3.getIntValue(1);
                    complete3 = completeObject3.getIntValue(1);
                }

                int total4 = 0;
                int complete4 = 0;
                if (taskAllOrders.size() > 0) {
                    JSONArray allObject4 = taskAllOrders.getJSONArray(i);
                    JSONArray completeObject4 = taskCompletedOrders.getJSONArray(i);
                    total4 = allObject4.getIntValue(1);
                    complete4 = completeObject4.getIntValue(1);
                }
                // M&R维修
                // 每日发单数合计
                int allTotal = total1 + total2 + total3+total4;
                // 每日完成数合计
                int allComplete = complete1 + complete2 + complete3+complete4;
                if (allTotal == 0) {
                    allCompleteArray.add(0);
                } else {
                    allCompleteArray.add((allComplete * 100 / allTotal * 100) / 100);
                }
                allTotalArray.add(allTotal);
            }
        }

        percentObject.put("data", allCompleteArray);
        percentObject.put("name", "completion rate（%）");
        percentObject.put("type", "line");
        percentObject.put("yAxisIndex", 1);

        result.put("percentObject", percentObject);
        result.put("dateTimes", dateTimes);
        return result;
    }
}
